package com.dorm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dorm.common.PageResult;
import com.dorm.common.Result;
import com.dorm.entity.Attendance;
import com.dorm.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 考勤控制器
 */
@RestController
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<PageResult<Attendance>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        Page<Attendance> page = attendanceService.pageQuery(pageNum, pageSize, studentId, type, startDate, endDate);
        PageResult<Attendance> result = new PageResult<>(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        return Result.success(result);
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result<Attendance> getById(@PathVariable Long id) {
        Attendance attendance = attendanceService.getById(id);
        return Result.success(attendance);
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<String> add(@RequestBody Attendance attendance) {
        attendanceService.save(attendance);
        return Result.success("添加成功");
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<String> update(@RequestBody Attendance attendance) {
        attendanceService.updateById(attendance);
        return Result.success("修改成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        attendanceService.removeById(id);
        return Result.success("删除成功");
    }
}

